<%--
  Created by IntelliJ IDEA.
  User:${author}
  Date: ${date}
  Time: ${time}
  desc: ${description}的展示界面
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
	<%@ include file="../../include/head.jsp"%>
	<%@ include file="../../include/layui.jsp"%>
	<title>${description}列表</title>
</head>
<body layadmin-themealias="default">
<div class="layui-fluid">
	<div class="layui-card">
		<div class="layui-form layui-card-header layuiadmin-card-header-auto">
			<!-- start 搜索框 -->
			<div class="layui-collapse">
				<div class="layui-colla-item">
					<h2 class="layui-colla-title">搜索操作</h2>
					<div class="layui-colla-content layui-show">
						<!-- 搜索相关表单及控件 参照`表单`样式 -->
						<form class="layui-form layui-form-pane" id="search-form">
							${queryList}
						</form>
					</div>
				</div>
			</div>
			<!-- end 搜索框 -->
		</div>
		<div class="layui-card-body" >
			<!-- start 表格内容 -->
			<div>
				<!-- start 顶部按钮 -->
				<div class="layui-btn-group btn-group-bottom" btn-listener>
					<shiro:hasPermission name="${module}:${nameLower}_add">
						<button class="layui-btn layui-btn-orange layui-btn-sm" lay-event="add">
							<i class="layui-icon">&#xe654;</i>添加
						</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="${module}:${nameLower}_del">
						<button class="layui-btn layui-btn-red layui-btn-sm" lay-event="delBatch" not-null="1">
							<i class="layui-icon">&#xe640;</i>删除
						</button>
					</shiro:hasPermission>
					<shiro:hasPermission name="${module}:${nameLower}_export">
						<button class="layui-btn layui-btn-green layui-btn-sm" lay-event="exportData">
							<i class="layui-icon">&#xe621;</i>导出
						</button>
					</shiro:hasPermission>
				</div>
				<!-- end 顶部按钮 -->
				<!-- start 表头按钮 -->
				<div class="table-header-btn-group" btn-listener>
					<shiro:hasPermission name="${module}:${nameLower}_turnOn">
						<a lay-event="turnOn" not-null="1"><i class="layui-icon"><span>&#xe652;</span>启用</i></a>
					</shiro:hasPermission>
					<shiro:hasPermission name="${module}:${nameLower}_turnOff">
						<a lay-event="turnOff" not-null="1"><i class="layui-icon"><span>&#xe651;</span>禁用</i></a>
					</shiro:hasPermission>
				</div>
				<!-- end 表头按钮 -->
				<table class="layui-hide" id="data-table" lay-filter="data-table"></table>
			</div>

			<!-- 行内操作按钮 -->
			<script type="text/html" id="toolBar">
				<a class="layui-btn-xs" title="查看" style="cursor:pointer" lay-event="detail"><i class="layui-icon" style="font-size: 40px; color: #1E9FFF;font-weight: bold">&#xe60a;</i></a>
				<shiro:hasPermission name="${module}:${nameLower}_modify">
					<a class="layui-btn-xs" title="编辑" style="cursor:pointer" lay-event="edit"><i class="layui-icon" style="font-size: 40px; color: #1E9FFF;font-weight: bold">&#xe642;</i></a>
				</shiro:hasPermission>
			</script>
			<!-- end 表格内容 -->
		</div>
	</div>
</div>

<script type="text/javascript">

    layui.use(['index', 'element', 'layer', 'table','laydate'], function(){
        var table = layui.table;
        ${dateQueryInput}
        //方法级渲染
        table.render({
            elem: '#data-table',
            url:  '${ctx}/${module}/${nameLower}/list',
            cols: [[
                ${viewList}
                ,{title: '操作', width: 90, toolbar: '#toolBar',fixed:'right'}
            ]]
            ,page: true
        });
        // 搜索按钮点事件
        $('#search-btn').on('click', function(){
            table.reload('data-table', {
                where: getParamJson('#search-form'),
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
        });

        // 校验初始化
        var form = layui.form;
        form.verify({
            ${queryVerify}
        });

        // 行操作事件定义
        var tableEvent = {
            detail: function(obj){
                var data = obj.data; //获得当前行数据
                var tr = obj.tr; //获得当前行 tr 的DOM对象
                openTabsPage('${ctx}/${module}/${nameLower}/detail?id=' + data.${pk}, '查看${description} - ' + data.${pk});
            },
            del: function(obj){
                var data = obj.data; //获得当前行数据
                parent.layer.confirm('真的删除行么', function(index){
                    parent.layer.close(index);
                    //向服务端发送删除指令
                    $.post('${ctx}/${module}/${nameLower}/del', {id: data.${pk}},function(result){
                        if(result.code == 0){
                            parent.layer.alert('操作成功', {
                                yes: function(index){
                                    parent.layer.close(index);
                                    obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
                                }
                            });
                        }else{
                            parent.layer.msg(result.msg || '操作失败');
                        }
                    }, 'json');
                });
            },
            edit: function(obj){
                var data = obj.data; //获得当前行数据
                openTabsPage('${ctx}/${module}/${nameLower}/edit?id=' + data.${pk}, '编辑${description} - ' + data.${pk});
            }
        };

        // 其他操作按钮事件定义
        var otherBtnEvent = {
            getPrimaryKeyParams: function(data){
                var params = '';
                $.each(data, function(i, item){
                    if(i !== 0) params += '&';
                    params += 'id=' + item.${pk};
                });
                return params
            },
            add: function(){
                openTabsPage('${ctx}/${module}/${nameLower}/newInfo?id=', '添加${description}');
            },
            delBatch: function(data, length, isAll){
                var params = otherBtnEvent.getPrimaryKeyParams(data);
                parent.layer.confirm('真的删除选中的行么', function(index){
                    parent.layer.close(index);
                    //向服务端发送删除指令
                    $.post('${ctx}/${module}/${nameLower}/del', params,function(result){
                        if(result.code == 0){
                            parent.layer.alert('操作成功', {
                                yes: function(index){
                                    parent.layer.close(index);
                                    // 更新表格
                                    table.reload('data-table');
                                }
                            });
                        }else{
                            parent.layer.msg(result.msg || '操作失败');
                        }
                    }, 'json');
                });
            },
            exportData: function(data, length, isAll){ //导出
                // 数量限制
                var params = $.param({
                    page: 1,
                    limit: 1000
                }, true);
                // 选中的数据
                var primaryKeys = otherBtnEvent.getPrimaryKeyParams(data);
                // 搜索参数

                // 聚合
                params = params + '&' + primaryKeys;
                window.open('${ctx}/${module}/${nameLower}/export?' + params);
            },
            turnOn: function(data, length, isAll){ // 启用
                var params = otherBtnEvent.getPrimaryKeyParams(data);
                $.post('${ctx}/${module}/${nameLower}/turnOn', params,function(result){
                    if(result.code == 0){
                        parent.layer.msg('操作成功');
                        // 更新表格
                        table.reload('data-table');
                    }else{
                        parent.layer.msg(result.msg || '操作失败');
                    }
                }, 'json');
            },
            turnOff: function(data, length, isAll){ // 停用
                var params = otherBtnEvent.getPrimaryKeyParams(data);
                $.post('${ctx}/${module}/${nameLower}/turnOff', params,function(result){
                    if(result.code == 0){
                        parent.layer.msg('操作成功');
                        // 更新表格
                        table.reload('data-table');
                    }else{
                        parent.layer.msg(result.msg || '操作失败');
                    }
                }, 'json');
            }
        };

        // 初始化监听事件
        initTableEvnet(table, 'data-table', tableEvent, otherBtnEvent);
    });

</script>
</body>
</html>
